home *** CD-ROM | disk | FTP | other *** search
/ Complete Linux / Complete Linux.iso / docs / devel / tcl / tclx7_31.z / tclx7_31 / tcldev / tclX7.3a-p1 / tests / testlib.tcl < prev    next >
Encoding:
Text File  |  1994-01-24  |  5.1 KB  |  164 lines

  1. #
  2. # testlib.tcl --
  3. #
  4. # Test support routines.  Some of these are based on routines provided with
  5. # standard Tcl.
  6. #------------------------------------------------------------------------------
  7. # Set the global variable TEST_ERROR_INFO to display errorInfo when a test
  8. # fails.
  9. #------------------------------------------------------------------------------
  10. # Copyright 1992-1993 Karl Lehenbauer and Mark Diekhans.
  11. #
  12. # Permission to use, copy, modify, and distribute this software and its
  13. # documentation for any purpose and without fee is hereby granted, provided
  14. # that the above copyright notice appear in all copies.  Karl Lehenbauer and
  15. # Mark Diekhans make no representations about the suitability of this
  16. # software for any purpose.  It is provided "as is" without express or
  17. # implied warranty.
  18. #------------------------------------------------------------------------------
  19. # $Id: testlib.tcl,v 3.2 1994/01/24 04:04:57 markd Exp $
  20. #------------------------------------------------------------------------------
  21. #
  22.  
  23. # Save the unknown command in a variable SAVED_UNKNOWN.  To get it back, eval
  24. # that variable.  Don't do this more than once.
  25.  
  26. global SAVED_UNKNOWN TCL_PROGRAM env
  27.  
  28. #
  29. # Save path to Tcl program to exec, use it when running children in the
  30. # tests.
  31. #
  32. if ![info exists TCL_PROGRAM] {
  33.     if [info exists env(TCL_PROGRAM)] {
  34.        set TCL_PROGRAM $env(TCL_PROGRAM)
  35.     } else {
  36.        puts stderr ""
  37.        puts stderr {WARNING: No environment variable TCL_PROGRAM,}
  38.        puts stderr {         assuming "tcl" as program to use for}
  39.        puts stderr {         running subprocesses in the tests.}
  40.        puts stderr ""
  41.        set TCL_PROGRAM tcl
  42.     }
  43. }
  44.  
  45. if {([info command unknown] == "") && ![info exists SAVED_UNKNOWN]} {
  46.     error "can't find either unknown or SAVED_UNKNOWN"
  47. }
  48. if {[info command unknown] != ""} {
  49.     set SAVED_UNKNOWN "proc unknown "
  50.     append SAVED_UNKNOWN "\{[info args unknown]\} "
  51.     append SAVED_UNKNOWN "\{[info body unknown]\}"
  52.     rename unknown {}
  53. }
  54.  
  55. #
  56. # Output a test error.
  57. #
  58. proc OutTestError {test_name test_description contents_of_test
  59.                    passing_int_result passing_result int_result result} {
  60.     global TEST_ERROR_INFO errorInfo
  61.     set int(0) TCL_OK
  62.     set int(1) TCL_ERROR
  63.     set int(2) TCL_RETURN
  64.     set int(3) TCL_BREAK
  65.     set int(4) TCL_CONTINUE
  66.  
  67.     puts stdout "==== $test_name $test_description"
  68.     puts stdout "==== Contents of test case:"
  69.     puts stdout "$contents_of_test"
  70.     puts stdout "==== Result was: $int($int_result)"
  71.     puts stdout "$result"
  72.     puts stdout "---- Result should have been: $int($passing_int_result)"
  73.     puts stdout "$passing_result"
  74.     puts stdout "---- $test_name FAILED" 
  75.     if {[info exists TEST_ERROR_INFO] && [info exists errorInfo]} {
  76.         puts stdout $errorInfo
  77.         puts stdout "---------------------------------------------------"
  78.     }
  79. }
  80.  
  81. #
  82. # Routine to execute tests and compare to expected results.
  83. #
  84. proc Test {test_name test_description contents_of_test passing_int_result
  85.            passing_result} {
  86.     set int_result [catch {uplevel $contents_of_test} result]
  87.  
  88.     if {($int_result != $passing_int_result) ||
  89.         ($result != $passing_result)} {
  90.         OutTestError $test_name $test_description $contents_of_test \
  91.                      $passing_int_result $passing_result $int_result $result
  92.     }
  93. }
  94.  
  95. #
  96. # Compare result against case-insensitive regular expression.
  97. #
  98.  
  99. proc TestReg {test_name test_description contents_of_test passing_int_result
  100.               passing_result} {
  101.     set int_result [catch {uplevel $contents_of_test} result]
  102.  
  103.     if {($int_result != $passing_int_result) ||
  104.         ![regexp -nocase $passing_result $result]} {
  105.         OutTestError $test_name $test_description $contents_of_test \
  106.                      $passing_int_result $passing_result $int_result $result
  107.     }
  108. }
  109.  
  110. proc dotests {file args} {
  111.     global TESTS
  112.     set savedTests $TESTS
  113.     set TESTS $args
  114.     source $file
  115.     set TESTS $savedTests
  116. }
  117.  
  118. # Genenerate a unique file record that can be verified.  The record
  119. # grows quite large to test the dynamic buffering in the file I/O.
  120.  
  121. proc GenRec {id} {
  122.     return [format "Key:%04d {This is a test of file I/O (%d)} KeyX:%04d %s" \
  123.                     $id $id $id [replicate :@@@@@@@@: $id]]
  124. }
  125.  
  126. # Proc to fork and exec child that loops until it gets a signal.
  127. # Can optionally set its pgroup.  Wait till child has actually execed or
  128. # kill breaks on some systems (i.e. AIX).
  129.  
  130. proc ForkLoopingChild {{setPGroup 0}} {
  131.     global TCL_PROGRAM
  132.     close [open CHILD.RUN w]
  133.     flush stdout
  134.     flush stderr
  135.     set newPid [fork]
  136.     if {$newPid != 0} {
  137.         # Wait till the child is actually running.
  138.         while {[file exists CHILD.RUN]} {
  139.             sleep 1
  140.         }
  141.         return $newPid
  142.     }
  143.     if $setPGroup {
  144.         id process group set
  145.     }
  146.     catch {
  147.         execl $TCL_PROGRAM \
  148.             {-qc {unlink CHILD.RUN; catch {while {1} {sleep 1}}; exit 10}}
  149.     } msg
  150.     puts stderr "execl failed (ForkLoopingChild): $msg"
  151.     exit 1
  152. }
  153.  
  154. #
  155. # Create a file.  If the directory doesn't exist, create it.
  156. #
  157. proc tcltouch file {
  158.     if ![file exists [file dirname $file]] {
  159.         mkdir -path [file dirname $file]
  160.     }
  161.     close [open $file w]
  162. }
  163.  
  164.